package com.link2room.rodimus.controller.manager;

import com.link2room.common.dto.result.base.BaseResultDto;
import com.link2room.sideswipe.permissionResouces.annotation.L2RPermissionResources;
import com.link2room.rodimus.dto.param.api.GroupBuyingTeamFindParamDto;
import com.link2room.rodimus.dto.param.base.GroupBuyingManualRefundParamDto;
import com.link2room.rodimus.dto.param.manager.GroupBuyingCreateManagerParamDto;
import com.link2room.rodimus.dto.param.manager.GroupBuyingListManagerParamDto;
import com.link2room.rodimus.dto.param.manager.GroupBuyingTeamUpdateParam;
import com.link2room.rodimus.dto.param.manager.GroupBuyingUpdateManagerParamDto;
import com.link2room.rodimus.facade.IGroupBuyingFacadeService;
import com.link2room.rodimus.facade.IGroupBuyingManagerFacadeService;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * Created by LN on 2017/12/5
 */
@Api(tags = "团购相关")
@RestController
@RequestMapping("/manager/GroupBuying")
public class GroupBuyingManagerController {

    @Resource
    private IGroupBuyingManagerFacadeService groupBuyingManagerFacadeService;
    @Resource
    private IGroupBuyingFacadeService groupBuyingFacadeService;

    @RequestMapping(method = RequestMethod.POST)
    @L2RPermissionResources(value = "createGroupBuyingBase", name = "创建团购活动")
    public BaseResultDto create(@RequestBody GroupBuyingCreateManagerParamDto paramDto) throws Exception {
        groupBuyingManagerFacadeService.createGroupBuying(paramDto);
        BaseResultDto resDto = new BaseResultDto();
        resDto.setResultCode(0);
        return resDto;
    }

    @RequestMapping(path = "/{code}", method = RequestMethod.PUT)
    @L2RPermissionResources(value = "updateGroupBuyingBase", name = "修改团购活动")
    public BaseResultDto update(
            @RequestBody GroupBuyingUpdateManagerParamDto paramDto,
            @PathVariable("code") String code) throws Exception {
        paramDto.setCode(code);
        groupBuyingManagerFacadeService.updateGroupBuying(paramDto);
        BaseResultDto resDto = new BaseResultDto();
        resDto.setResultCode(0);
        return resDto;
    }

    @RequestMapping(path = "/{code}", method = RequestMethod.GET)
    @L2RPermissionResources(value = "getGroupBuying", name = "查看团购活动详情")
    public BaseResultDto getGroupBuying(
            @PathVariable("code") String code
    ) throws Exception {
        GroupBuyingListManagerParamDto paramDto = new GroupBuyingListManagerParamDto();
        paramDto.setGroupBuyingCode(code);

        BaseResultDto resDto = new BaseResultDto();
        resDto.setResultInfo(groupBuyingManagerFacadeService.groupBuyingBaseDto(paramDto));
        resDto.setResultCode(0);
        return resDto;
    }

    @RequestMapping(method = RequestMethod.GET)
    @L2RPermissionResources(value = "groupBuyingList", name = "查看团购活动列表")
    public BaseResultDto groupBuyingList(
            @RequestParam(name = "sta") String sta,
            @RequestParam(name = "goodCode", required = false) String goodCode,
            @RequestParam(name = "order", required = false) String order,
            @RequestParam(name = "sortBy", required = false) String sortBy,
            @RequestParam(name = "isHalt", required = false) String isHalt
    ) throws Exception {
        GroupBuyingListManagerParamDto paramDto = new GroupBuyingListManagerParamDto();
        paramDto.setSta(sta);
        paramDto.setGoodCode(goodCode);
        paramDto.setIsHalt(isHalt);
        paramDto.setSortBy(sortBy);
        paramDto.setOrder(order);
        BaseResultDto resDto = new BaseResultDto();
        resDto.setResultCode(0);
        resDto.setQueryResult(groupBuyingManagerFacadeService.listGroupBuyingBaseDto(paramDto));
        return resDto;
    }

    @RequestMapping(path = "/groupBuyingTeams", method = RequestMethod.GET)
    @L2RPermissionResources(value = "groupBuyingTeams", name = "团购活动团队列表")
    public BaseResultDto groupBuyingTeams(
            @RequestParam(name = "teamCode", required = false) String teamCode,
            @RequestParam(name = "groupBuyingCode", required = false) String groupBuyingCode,
            @RequestParam(name = "sta", required = false) String sta,
            @RequestParam(name = "catpatinNickName", required = false) String catpatinNickName,
            @RequestParam(name = "catpatinOpenid", required = false) String catpatinOpenid
    ) throws Exception {
        GroupBuyingTeamFindParamDto paramDto = new GroupBuyingTeamFindParamDto();
        paramDto.setCatpatinNickName(catpatinNickName);
        paramDto.setCatpatinOpenid(catpatinOpenid);
        paramDto.setSta(sta);
        paramDto.setTeamCode(teamCode);
        paramDto.setGroupBuyingCode(groupBuyingCode);
        BaseResultDto resDto = new BaseResultDto();
        resDto.setResultCode(0);
        resDto.setQueryResult(groupBuyingFacadeService.groupBuyingTeamList(paramDto));
        return resDto;
    }

    @RequestMapping(path = "/groupBuyingTeams/{teamCode}", method = RequestMethod.GET)
    @L2RPermissionResources(value = "groupBuyingTeam", name = "团购活动团队详情")
    public BaseResultDto groupBuyingTeam(
            @PathVariable("teamCode") String teamCode
    ) throws Exception {
        GroupBuyingTeamFindParamDto paramDto = new GroupBuyingTeamFindParamDto();
        paramDto.setTeamCode(teamCode);
        BaseResultDto resDto = new BaseResultDto();
        resDto.setResultCode(0);
        resDto.setResultInfo(groupBuyingManagerFacadeService.groupBuyingTeam(paramDto));
        return resDto;
    }


    @RequestMapping(path = "/groupBuyingTeams/{teamCode}", method = RequestMethod.PUT)
    @L2RPermissionResources(value = "updateGroupBuyingTeam", name = "团购活动团队状态修改")
    public BaseResultDto updateGroupBuyingTeam(
            @PathVariable("teamCode") String teamCode,
            @RequestBody GroupBuyingTeamUpdateParam paramDto
    ) throws Exception {
        paramDto.setTeamCode(teamCode);
        groupBuyingManagerFacadeService.updateGroupBuyingTeam(paramDto);
        BaseResultDto resDto = new BaseResultDto();
        resDto.setResultCode(0);
        return resDto;
    }


    @RequestMapping(path = "/groupBuyingTeams/{teamCode}/{orderNo}", method = RequestMethod.POST)
    @L2RPermissionResources(value = "groupBuyingRefund", name = "团购活动退款")
    public BaseResultDto groupBuyingRefund(
            @PathVariable("teamCode") String teamCode,
            @PathVariable("orderNo") String orderNo) throws Exception {
        GroupBuyingManualRefundParamDto paramDto = new GroupBuyingManualRefundParamDto();
        paramDto.setOrderNo(orderNo);
        paramDto.setTeamCode(teamCode);
        groupBuyingManagerFacadeService.manualRefund(paramDto);
        BaseResultDto resDto = new BaseResultDto();
        resDto.setResultCode(0);
        return resDto;
    }


}
